#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""

@author ：chenzf
@date ：2024/4/23 下午12:03 
"""
from pymysql import Connection

# 连接数据库
conn = Connection(host="192.168.108.100", port=3306, user="root", password="123456", autocommit=True)
cursor = conn.cursor()  # 获取游标
try:
    conn.select_db("dev_new") # 选择数据库
    # 创建表
    cursor.execute("create table if not exists student(id int primary key auto_increment, name varchar(20), age int)")
    # 插入数据
    # cursor.execute("insert into student(name, age) values('zhangsan', 18)")
    # cursor.execute("insert into student(name, age) values('lisi', 19)")
    # cursor.execute("insert into student(name, age) values('wangwu', 20)")
    # 提交事务
    conn.commit()
    cursor.execute("select * from student")
    for row in cursor:
        print(row)
    # cursor.execute("delete from student where id = 1")
except EOFError as e:
    conn.rollback()
    print(e)
finally:
    cursor.close()
    conn.close()
